package com.cn.longdistancebusstation.personal;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.cn.longdistancebusstation.R;
import com.cn.longdistancebusstation.adapter.CouponAdapter;
import com.cn.longdistancebusstation.adapter.CouponAdapter2;
import com.cn.longdistancebusstation.base.BaseActivity;
import com.cn.longdistancebusstation.callback.OnCouponListener;
import com.cn.longdistancebusstation.callback.ScrollViewListener;
import com.cn.longdistancebusstation.global.GlobalVariable;
import com.cn.longdistancebusstation.httpHelper.HttpHelper;
import com.cn.longdistancebusstation.httpapi.HttpService;
import com.cn.longdistancebusstation.model.Coupons;
import com.cn.longdistancebusstation.model.HistoryCoupons;
import com.cn.longdistancebusstation.model.ListResult;
import com.cn.longdistancebusstation.model.ListResultII;
import com.cn.longdistancebusstation.order.ConfirmOrderActivity;
import com.cn.longdistancebusstation.view.MyListView;
import com.cn.longdistancebusstation.view.MyScrollView;
import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * Created by mucheng on 2018/2/26.
 * 因为scrollview的问题，所以需要手动设定listview的高度。
 * 在listview1滑动到底部时需要屏蔽listview1的滑动，让scrollview滑动。
 * 在listview2滑动到顶部时屏蔽listview2的滑动，让scrollview滑动。
 * 在scrollview滚动中，屏蔽listview的滚动。
 */

public class CouponsActivity extends BaseActivity implements AdapterView.OnItemClickListener,OnCouponListener{
//    private MyScrollView mScrollView;
    private LinearLayout mLinearLayout;
    private MyListView mCouponListView;
    private MyListView mListView;
    private CouponAdapter mCouponAdapter;
    private CouponAdapter2 mCouponAdapter2;
    private List<Coupons>mCouponsList=new ArrayList<>();
    private LinearLayout mLinearLayout1;
    private LinearLayout mLinearLayout2;
    private Coupons mCoupons;
    private String mCouponString;
    private List<CheckBox> checkBoxList=new ArrayList<CheckBox>();
    private String strCouponId;
    private TextView mTextView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coupons);
        this.getTitleTextView().setText("优惠券");
        this.showMenu1("确定",0);
        Intent intent=getIntent();
        strCouponId=intent.getStringExtra("couponid");
        initView();
        getCoupons();

//        setListViewHeightBasedOnChildren(mCouponListView);
//        setListViewHeightBasedOnChildren2(mListView);
    }



    private void initView() {
        mLinearLayout=(LinearLayout)findViewById(R.id.main_layout);
        mCouponListView=(MyListView)findViewById(R.id.coupons_list);
        mTextView=(TextView)findViewById(R.id.textview_coupon_history);
        mCouponAdapter=new CouponAdapter(this,mCouponsList,strCouponId);
        mCouponListView.setAdapter(mCouponAdapter);
        mCouponAdapter.setOnCouponListener(this);
        mListView=(MyListView)findViewById(R.id.coupons_list2);
        mCouponAdapter2 =new CouponAdapter2(CouponsActivity.this, mCouponsList);
        mListView.setAdapter(mCouponAdapter2);

    }
    private void getCoupons() {
        mCouponsList.clear();
        Retrofit retrofit=new HttpHelper.HttpServiceBuilder().build().getRetrofit();
        HttpService httpService=retrofit.create(HttpService.class);
        Call<ListResultII<Coupons>> call=httpService.getCoupons(GlobalVariable.getUserID());
        Log.d("用户Id",GlobalVariable.getUserID());
        showHud();
        call.enqueue(new Callback<ListResultII<Coupons>>() {
            @Override
            public void onResponse(Call<ListResultII<Coupons>> call, Response<ListResultII<Coupons>> response) {
                hideHud();
                ListResultII result = response.body();
                if(result.getHead().getSuccess()){
                    mCouponsList.addAll(result.getData());
                    mCouponAdapter.setCouponsList(result.getData());
                    mCouponAdapter2.setCouponsList(result.getData());
                    Log.d("优惠券",mCouponsList.size()+"");

                    mCouponAdapter.notifyDataSetChanged();
                    mCouponAdapter2.notifyDataSetChanged();

                }else{
                    Toast.makeText(CouponsActivity.this,"获取数据失败，请重新获取",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<ListResultII<Coupons>> call, Throwable t) {
                hideHud();

            }
        });
    }
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    }

    @Override
    public void onClick(int position) {
        Coupons coupon=mCouponsList.get(position);
        boolean checked = coupon.isSelected();
        if(checked==true){
            coupon.setSelected(true);
        }else{
            coupon.setSelected(false);
        }
    }
    @Override
    public void onMenu1Click() {
        super.onMenu1Click();
        sure();
    }

    private void sure() {
        for(int i=0;i<mCouponsList.size();i++){
            Coupons couponlist=mCouponsList.get(i);
            Intent in = new Intent(CouponsActivity.this, ConfirmOrderActivity.class);
            in.putExtra("coupon", couponlist);
            setResult(RESULT_OK, in);
            finish();
        }
    }
}
